from django.shortcuts import render
from db.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import random
from django.core.cache import cache

from django_redis import get_redis_connection

from utils.login_verify import verify


def index(request):
    """
    展示五类商品(按照每个商品的类型以及上下架)，主页面每次随机8个
    """
    # 男装
    # cache.delete('index_page')
    # print('删除缓存...')
    context = cache.get('index_page')
    if context is None:
        print('设置缓存...')
        # 缓存商品的种类信息
        boy = Goods.objects.filter(goodstype_id=1, status=1).values()
        boys = random.sample(list(boy), 8)
        # 女装
        girl = Goods.objects.filter(goodstype_id=2, status=1).values()
        girls = random.sample(list(girl), 8)
        # 童装
        kid = Goods.objects.filter(goodstype_id=3, status=1).values()
        kids = random.sample(list(kid), 8)
        # 包包
        bag = Goods.objects.filter(goodstype_id=4, status=1).values()
        bags = random.sample(list(bag), 8)
        # 鞋靴
        shoe = Goods.objects.filter(goodstype_id=5, status=1).values()
        shoes = random.sample(list(shoe), 8)
        # 爆款(每次随机3个)
        a = random.sample(list(girl), 3)
        b = random.sample(list(boy), 3)

        # 购物车商品数量
        user = request.user
        # print(user)
        if user.is_authenticated:
            cart_key = 'cart_%d' % user.id
            conn = get_redis_connection('default')
            cart_num = conn.hlen(cart_key)
        else:
            cart_num = 0
        # user = request.session['phone']
        # print('-------------', user)
        context = {
            # 'user': user,
            'boys': boys,
            'girls': girls,
            'kids': kids,
            'bags': bags,
            'shoes': shoes,
            'a': a, 'b': b,
            'cart_num': cart_num}

        # 用户信息
        try:
            phone = request.session['phone']
        except:
            user = ''
        else:
            user = User.objects.get(phone=phone)

        context = {'boys': boys, 'girls': girls, 'kids': kids, 'bags': bags, 'shoes': shoes, 'a': a, 'b': b,
                   'user_info': user, 'cart_num': cart_num}


        # 设置缓存
        # key value timeout
        cache.set('index_page', context, 120)

    return render(request, 'goods/index.html', context)


def item_show(request, qid):
    goods = Goods.objects.get(pk=qid)
    # 男装
    boy = Goods.objects.filter(goodstype_id=1, status=1).values()
    boys = random.sample(list(boy), 3)
    boyss = random.sample(list(boy), 5)
    # 女装
    girl = Goods.objects.filter(goodstype_id=2, status=1).values()
    girls = random.sample(list(girl), 3)
    girlss = random.sample(list(girl), 5)
    # 童装
    kid = Goods.objects.filter(goodstype_id=3, status=1).values()
    kids = random.sample(list(kid), 3)
    kidss = random.sample(list(kid), 5)
    # 包包
    bag = Goods.objects.filter(goodstype_id=4, status=1).values()
    bags = random.sample(list(bag), 3)
    bagss = random.sample(list(bag), 5)
    # 鞋靴
    shoe = Goods.objects.filter(goodstype_id=5, status=1).values()
    shoes = random.sample(list(shoe), 3)
    shoess = random.sample(list(shoe), 5)

    return render(request, 'goods/item_show.html', locals())


# 商品详情页
def item_category(request, gid):
    """
    gid:1:男装 2:女装 3:童装 4：包包 5：鞋靴
    """
    # cache.delete('item_category_page')
    # context = cache.get('item_category_page')
    # if context is None:
    # 颜色
    color = Color.objects.all()
    # 大小
    size = Size.objects.all()
    # 商品query对象
    goods = Goods.objects.filter(goodstype_id=gid, status=1).values()
    # 商品总个数
    length = len(goods)
    # 分页
    page = request.GET.get('page')
    page_obj, page_num = get_page(page, goods, 16)
    page_num = list(range(1, page_num + 1))
    # 爆款
    fiddsh = random.sample(list(goods), 7)
    context = {
        'goods': goods,
        'page': page,
        'length': length,
        'page_obj': page_obj,
        'page_num': page_num,
        'fiddsh': fiddsh,
        'color': color,
        'size': size,
        'gid': gid}

    # cache.set('item_category_page', context, 120)
    return render(request, 'goods/item_category.html', context)


def item_sale_page(request):
    good = Goods.objects.all()
    goods = random.sample(list(good), 300)
    length = len(goods)
    goodss = random.sample(list(good), 10)
    # 分页
    page = request.GET.get('page')
    page_obj, page_num = get_page(page, goods, 20)
    page_num = list(range(1, page_num + 1))
    context = {
        'goods': goods,
        'length': length,
        'page': page,
        'page_obj': page_obj,
        'page_num': page_num,
        'goodss': goodss,
    }
    return render(request, 'goods/item_sale_page.html', context)


def item_remove(request):
    goods = Goods.objects.filter(status=0)
    return render(request, 'goods/item_remove.html', context={'goods': goods})


def get_page(page=1, list_obj=None, page_size=30):  # 分页器
    """
    用于分页显示数据
    :param page: 页数
    :param list_obj: 所有数据对象优先list
    :param page_size: 每页多少条数据
    :return: [[选择的页数数据], 页数]
    """
    paginator = Paginator(list(list_obj), page_size)  # 在这里调整每一页几条数据
    try:
        return paginator.page(page), paginator.num_pages
    except PageNotAnInteger:  # 如果页面不是整数，则提交第一页。
        return paginator.page(1), paginator.num_pages
    except EmptyPage:  # 如果页面超出范围(例如9999)，提交结果的最后一页。
        return paginator.page(paginator.num_pages), paginator.num_pages
